//抽象语法树测试
let esprima = require('esprima');
let escodegen = require('escodegen');

const fs = require('fs');
const csstree = require('css-tree');

const encodeJs = function () {
    const str = `
    global.x = 10;
    name += '-abc';
    age = 12;
    `;
    const AST = esprima.parse(str);
    // console.log(AST);
    // console.log(AST.body[0].expression.left.object.name);
    // console.log(result.body[0].consequent.body[0].expression.left);

    AST.body.splice(0, 1);

    let originReback = escodegen.generate(AST);
    console.log(originReback);
};

// encodeJs();

const getArr = function (item, arr) {
    if (item.children) {
        item.children.forEach((child) => {
            getArr(child, arr);
        });
        return;
    }
    if (!item.value || item.value === ',') {
        return
    }
    console.log(item.value)
    var tempArr = item.value.replace(/"/g, `'`).split(`'`).filter(Boolean);
    arr.push(...tempArr);
}

const encodeCss = function () {
    // let cssString = `@import('@kiss/go')`;
    let cssString = `
    @kiss('go');
    @kiss("search");
    @kiss(['ab','cd']);
    a.button {
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
      }
      @media print {
        #header,
        #footer {
          display: none;
        }
      }
    `;
    let ast = csstree.parse(cssString);
    console.log(ast);
    // let copy = csstree.clone(ast);
    fs.writeFile('./dist/ast.json', JSON.stringify(ast), function () {
        console.log('ast分析代码写入到ast.json')
    })
    var arr = [];

    console.log(ast.children)
    var json = JSON.parse(JSON.stringify(ast));
    if (json.children) {
        for(let len= json.children.length, i = len-1;i>=0;i--){
            let node= json.children[i];
            if (node.type === 'Atrule' && node.name == 'kiss') {
                // node.value = (node.value / 1.745).toFixed(3)
                // console.log(node.prelude)
                if (node.prelude.type == 'AtrulePrelude') {
                    node.prelude.children.forEach((item) => {
                        getArr(item, arr)
                    });
                }
                json.children.splice(i, 1);
            }
        }

    }

    // csstree.walk(copy, function (node, item, list) {
    //     //console.log(node.type,'____', node.value)
    //     if (node.type === 'Atrule' && node.name == 'kiss') {
    //         // node.value = (node.value / 1.745).toFixed(3)
    //         // console.log(node.prelude)
    //         if (node.prelude.type == 'AtrulePrelude') {
    //             node.prelude.children.forEach((item) => {
    //                 getArr(item, arr)
    //             });
    //         }
    //     }
    // });
    console.log(arr)
    let transCode = csstree.generate(json)+''
    // transCode = transCode.replace(/rpx/g,'px')
    //  // console.log(transCode)
    fs.writeFile('./dist/transCode.css', transCode, function () {
        console.log('最终代码写入到transCode.css')
    })
};

encodeCss();